<?php
namespace App\Common\Cla\Weixin\Pay;

use App\Common as com;
use App\Common\Cla\Weixin\Pay as namePay;

class Refund extends namePay\Base\OfConfig {

	/**
	 * 根据微信订单号退款
	 * @param string $transactionId 微信订单号
	 * @param string $refundNumber 商户退款单号
	 * @param int $totalFee 订单金额
	 * @param int $refundFee 退款金额
	 * @param array $config 其他参数，可在此处传入其他参数，详细参数见微信支付文档
	 * @return array
	 * 返回数据示例：
	 * array(18) {
	 * 		["return_code"] => string(7) "SUCCESS"
	 * 		["return_msg"] => string(2) "OK"
	 * 		["appid"] => string(18) "wxa088e85a0d240726"
	 * 		["mch_id"] => string(10) "1602401752"
	 * 		["nonce_str"] => string(16) "wiAJzZdwPFa54oFb"
	 * 		["sign"] => string(32) "49A9E084CDBB07072F161ADA272FC4D7"
	 * 		["result_code"] => string(7) "SUCCESS"
	 * 		["transaction_id"] => string(28) "4200001946202309069694358443"
	 * 		["out_trade_no"] => string(20) "OC169401507724123837"
	 * 		["out_refund_no"] => string(2) "A1"
	 * 		["refund_id"] => string(29) "50301306762023090787386406717"
	 * 		["refund_channel"] => NULL
	 * 		["refund_fee"] => string(1) "1"
	 * 		["coupon_refund_fee"] => string(1) "0"
	 * 		["total_fee"] => string(1) "1"
	 * 		["cash_fee"] => string(1) "1"
	 * 		["coupon_refund_count"] => string(1) "0"
	 * 		["cash_refund_fee"] => string(1) "1"
	 * }
	 * @throws
	 */
	public function byTransactionId( string $transactionId, string $refundNumber, int $totalFee, int $refundFee, array $config = [] ):array {
		//
		$apiRes = $this->getEasyObj()->refund->byTransactionId( $transactionId, $refundNumber, $totalFee, $refundFee, $config );
		// 检查结果
		if( $apiRes['result_code'] !== 'SUCCESS' ){
			throw com\Lib\Err\Exception::makeOfNew( '退款失败，' . $unifyRes['result_code'], com\Lib\Err\Code::$sysAssertError );
		}
		//
		return $apiRes;
	}

	/**
	 * 根据商户订单号退款
	 * @param string $number 商户订单号
	 * @param string $refundNumber 商户退款单号
	 * @param int $totalFee 订单金额
	 * @param int $refundFee 退款金额
	 * @param array $config 其他参数，可在此处传入其他参数，详细参数见微信支付文档
	 * @return array|string [array]成功，返回数据示例参考 $this->byOutTradeNumberReArray() [string]失败，并返回错误提示（返回“err_code_des”字段）
	 * @throws
	 */
	public function byOutTradeNumberReArrayOrLoseString( string $number, string $refundNumber, int $totalFee, int $refundFee, array $config = [] ):array|string {
		$apiRes = $this->byOutTradeNumberReArray( $number, $refundNumber, $totalFee, $refundFee, $config );
		// 检查结果
		if( $apiRes['result_code'] !== 'SUCCESS' ){
			return $apiRes['err_code_des'];
		}
		//
		return $apiRes;
	}

	/**
	 * @param string $number
	 * @param string $refundNumber
	 * @param int $totalFee
	 * @param int $refundFee
	 * @param array $config
	 * @return array
	 * 返回数据示例：
	 * array(18) {
	 * 		["return_code"] => string(7) "SUCCESS"
	 * 		["return_msg"] => string(2) "OK"
	 * 		["appid"] => string(18) "wxa088e85a0d240726"
	 * 		["mch_id"] => string(10) "1602401752"
	 * 		["nonce_str"] => string(16) "wiAJzZdwPFa54oFb"
	 * 		["sign"] => string(32) "49A9E084CDBB07072F161ADA272FC4D7"
	 * 		["result_code"] => string(7) "SUCCESS"
	 * 		["transaction_id"] => string(28) "4200001946202309069694358443"
	 * 		["out_trade_no"] => string(20) "OC169401507724123837"
	 * 		["out_refund_no"] => string(2) "A1"
	 * 		["refund_id"] => string(29) "50301306762023090787386406717"
	 * 		["refund_channel"] => NULL
	 * 		["refund_fee"] => string(1) "1"
	 * 		["coupon_refund_fee"] => string(1) "0"
	 * 		["total_fee"] => string(1) "1"
	 * 		["cash_fee"] => string(1) "1"
	 * 		["coupon_refund_count"] => string(1) "0"
	 * 		["cash_refund_fee"] => string(1) "1"
	 * }
	 */
	public function byOutTradeNumberReArray( string $number, string $refundNumber, int $totalFee, int $refundFee, array $config = [] ):array {
		//
		$apiRes = $this->getEasyObj()->refund->byOutTradeNumber( $number, $refundNumber, $totalFee, $refundFee, $config );
		// 检查结果
/*		if( $apiRes['result_code'] !== 'SUCCESS' ){
			d([
				'退款失败',
				$apiRes
			]);
		}*/
		//
		return $apiRes;
	}

}
